/* 
 * File:   MeangleFactory.cpp
 * Author: maximiliano
 * 
 * Created on March 26, 2013, 5:42 PM
 */
#include <sstream>
#include "MangleFactory.h"

#include "UpperCase.h"
#include "LowerCase.h"
#include "Print.h"
#include "Repeat.h"
#include "Rotate.h"
#include "Revert.h"
#include "Insert.h"

using namespace std;
using namespace TP3::Mangling;

MangleFactory::MangleFactory() {
}

MangleFactory::~MangleFactory() {
}

/*
 * uppercase
 * lowercase
 * repeat
 * rotate
 * insert
 * revert
 * print
 * 
 * 
*/

Mangle* MangleFactory::create (Descriptor& descriptor) throw (int) {
    
    if ( descriptor.getName() == "uppercase")
        return createUppercase (descriptor);
    
    if ( descriptor.getName() == "lowercase")
        return createLowercase (descriptor);
    
    if ( descriptor.getName() == "repeat")
        return createRepeat (descriptor);
    
    if ( descriptor.getName() == "rotate")
        return createRotate (descriptor);
    
    if ( descriptor.getName() == "insert")
        return createInsert (descriptor);
    
    if ( descriptor.getName() == "revert")
        return createRevert (descriptor);
    
    if ( descriptor.getName() == "print")
        return createPrint (descriptor);
    
    return NULL;
}

Mangle* MangleFactory::createUppercase (Descriptor& descriptor){
    
    int posIni, posEnd;

    std::stringstream reader;
    reader << descriptor.getParameters()[0];
    reader >> posIni;

    reader.clear();
    reader << descriptor.getParameters()[1];
    reader >> posEnd;
    
    return new UpperCase(posIni, posEnd);
}

Mangle* MangleFactory::createLowercase (Descriptor& descriptor){

    int posIni, posEnd;

    std::stringstream reader;
    reader << descriptor.getParameters()[0];
    reader >> posIni;

    reader.clear();
    reader << descriptor.getParameters()[1];
    reader >> posEnd;
    
    return new LowerCase(posIni, posEnd);

}

Mangle* MangleFactory::createRepeat (Descriptor& descriptor){
    
    // Repeat(int posIni, int posEnd, int times, int posInsert);
    int posIni, posEnd, times, posInsert;
    
    std::stringstream reader;
    reader << descriptor.getParameters()[0];
    reader >> posIni;

        
    reader.clear();
    reader << descriptor.getParameters()[1];
    reader >> posEnd;

    reader.clear();
    reader << descriptor.getParameters()[2];
    reader >> times;

    reader.clear();
    reader << descriptor.getParameters()[3];
    reader >> posInsert;
    
    return (Mangle*) new Repeat (posIni, posEnd, times, posInsert);
    
}

Mangle* MangleFactory::createRotate (Descriptor& descriptor){

    int placesNumber;

    std::stringstream reader;
    reader << descriptor.getParameters()[0];
    reader >> placesNumber;
    
    return (Mangle*) new Rotate (placesNumber);
}

Mangle* MangleFactory::createInsert (Descriptor& descriptor){
    
    int pos;

    std::stringstream reader;
    reader << descriptor.getParameters()[0];
    reader >> pos;

    return new Insert (pos, descriptor.getParameters()[1]);
}

Mangle* MangleFactory::createRevert (Descriptor& descriptor){

    int times;

    std::stringstream reader;
    reader << descriptor.getParameters()[0];
    reader >> times;
    
    return (Mangle*) new Revert (times);
}

Mangle* MangleFactory::createPrint (Descriptor& descriptor){
    
    return (Mangle*) new Print();
}  
